
<script>
/* eslint-disable */

import Node from "./Node";

export default {
  name: "Particle",

  mixins: [Node],

  props: {
    fileasset:{type:Object,default:()=>null},
    playcount:{type:Number,default:0},
  },
  watch: {
    nodeProps: {
      handler(newv, oldv) {
        // console.log("sprite watch nodeProps",newv,oldv);
        let oldProps = this.curProps;
        this.curProps = this.mergeProps();
        this._updateNodeProps(this.ccnode, oldProps, this.curProps);
      },
    },
    nodeComps: {
      handler(newv, oldv) {
        // console.log("sprite watch nodeComps",newv,oldv);
        let oldComps = this.curComps;
        this.curComps = this.mergeComps();
        this._updateNodeComps(this.ccnode, oldComps, this.curComps);
      },
    },
    playcount: {
      handler(newv, oldv) {
        // console.log("particle watch playcount",newv,oldv);
        if(newv === oldv){
          return;
        }
        let comp = this.ccnode.getComponent(cc.ParticleSystem);
        if (newv) {
            comp.resetSystem();
        } else {
            comp.stopSystem();
        }
      },
    },
  },

  created() {
    // console.log("particle created",this.dict,this.playcount);
    this.createParticle();
  },

  methods: {
    //////////////////////////
    // 下面函数替换Node
    createNode() {},
    updateNodeProps(node, oldp, newp) {},
    updateNodeComps(node, oldp, newp) {},

    //////////////////////////
    mergeProps() {
      return this.nodeProps;
    },
    mergeComps() {
      let comps = [
        this.ptcComp,
      ];
      if (this.nodeComps) {
        this.nodeComps.forEach((c) => {
          if (c && c.type && c.type !== cc.ParticleSystem ) {
            comps.push(c);
          }
        });
      }
      return comps;
    },
    createParticle() {
      if (!this.ccnode) {
        this.ptcComp = {
          type: cc.ParticleSystem,
          props: {
            file:this.fileasset,
            playOnLoad:this.playcount!==0
          },
        }        
        this.curComps = this.mergeComps();
        this.curProps = this.mergeProps();
        this._createNode(this.curProps, this.curComps);
      }
    },
  },
};
</script>
